<?php
Yii::import('widgets.portal.HomePortlet');

class EmployedYearNotification extends HomePortlet {
	public $title = '工龄提醒';
	protected $role = 'employeeView';

	public $dayOffsetMin = -2;
	public $dayOffsetMax = 10;
	public $employeeLimit = 10;

	protected function renderContent() {
		$criteria = new CDbCriteria();
		$criteria->compare('employed', 1);
		$criteria->select = '*, ' . "DATEDIFF(DATE_FORMAT(employedDate, concat((year(now())+"
			. "IF(DAYOFYEAR(NOW())>DAYOFYEAR(employedDate)+100,1,0)"
			. "),'-%m-%d')), date(now())) as employedDateOffset, ROUND(DATEDIFF(NOW(),employedDate)/365.0) as employedYear";
		$criteria->order = 'employedDateOffset ASC';
		$criteria->having = 'employedDateOffset between :dayOffsetMin and :dayOffsetMax AND employedYear > 0';
		$criteria->params['dayOffsetMin'] = $this->dayOffsetMin;
		$criteria->params['dayOffsetMax'] = $this->dayOffsetMax;

		$model = Employee::model();
		$commandBuilder = Yii::app()->db->getSchema()->getCommandBuilder();
		$command = $commandBuilder->createFindCommand($model->getTableSchema(), $criteria);
		$rows = $command->queryAll();

		$employeeWithEmployedDate = array();
		foreach ($rows as $row) {
			$employee = new Employee();
			$employee->setAttributes($row, false);
			$employeeWithEmployedDate[] = array(
				'employee' => $employee,
				'employedDateOffset' => $row['employedDateOffset'],
				'employedYear' => $row['employedYear'],
			);
		}
		$this
			->render('employedDateNotification',
				array(
					'employeeWithEmployedDate' => $employeeWithEmployedDate
				));
	}
}
